package org.yibinxueyuan.drawcard.service.serivceImpl;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Service;
import org.yibinxueyuan.drawcard.entity.Cardinfo;
import org.yibinxueyuan.drawcard.mapper.CardInfoMapper;
import org.yibinxueyuan.drawcard.mapper.CommonToolsDao;
import org.yibinxueyuan.drawcard.pojo.Paging;
import org.yibinxueyuan.drawcard.service.CardInfoService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 卡片管理 服务层实现类
 */
@Service(value = "CardinfoServiceImpl")
public class CardinfoServiceImpl extends ApplicationObjectSupport implements CardInfoService {
    @Resource(name = "CardInfoMapper")
    private CardInfoMapper cardInfoMapper;
    @Resource(name = "commonToolsDaoImpl")
    private CommonToolsDao commonTools;

    @Override
    public List<Cardinfo> queryAllCardByPage(Paging paging) {
        List<Cardinfo> cardinfos = cardInfoMapper.queryAllCardByPage(paging);
        return cardinfos;
    }

    @Override
    public int queryAllTotalCardNumber(String cardName) {
        int number = cardInfoMapper.queryAllTotalCardNumber(cardName);
        return number;
    }

    @Override
    public int insertNewCard(Cardinfo cardinfo) {
        /**
         * 这里假设了抽卡概率不能为空,如果为空,就应该会出问题
         * 所以前面一定得有抽卡概率不为空的保证,
         * 毕竟,增加数据抽卡概率不能为空的
         */
        int number = -1;
        //检查值是否符合规范
        if(cardinfo != null && cardinfo.getCardChance() != null && cardinfo.getCardChance() != -1
            && cardinfo.getCardAddress() != null && cardinfo.getCardName()!= null
            && cardinfo.getGameName() != null && cardinfo.getCardLevel() != null
            && cardinfo.getCardLevel() != -1) {
            //获取最大的终点值
            int maxl = cardInfoMapper.queryMaxCardChancel();
            //如果为起点和终点为空或者为""空字符,自动赋值给起点和终点
            if (cardinfo.getCardChancel() == null || cardinfo.getCardChancel() == -1) {
                cardinfo.setCardChancel(maxl + cardinfo.getCardChance());
            }
            if (cardinfo.getCardChancef() == null || cardinfo.getCardChancef() == -1) {
                cardinfo.setCardChancef(maxl);
            }

            //如果id值不为空,且在数据库中不存在现在这样的一个id值,则添加数据
            if (cardinfo.getCardId() != null && cardInfoMapper.queryCardById(cardinfo.getCardId()) == null) {
                number = cardInfoMapper.insertNewCard(cardinfo);
                //如果id值不存在,则自动增加设置id值
            } else if (cardinfo.getCardId() == null) {
                number = cardInfoMapper.insertNewCard(cardinfo);
            }
        }
        return number;
    }

    @Override
    public int updateCardinfoById(Cardinfo cardinfo) {
        int number = -1;
        //检查数据是否存在 存在则进行修改数据的操作
        if(cardinfo != null && cardinfo.getCardId() != null && cardInfoMapper.queryCardById(cardinfo.getCardId()) != null){
            number = cardInfoMapper.updateCardinfoById(cardinfo);
        }
        return number;
    }

    @Override
    public Cardinfo queryCardById(int cardId) {
        return cardInfoMapper.queryCardById(cardId);
    }

    @Override
    public int deleteCardinfoById(int cardId) {
        int number = -1;
        //检查数据是否存在 存在则进行修改数据的操作
        if(cardInfoMapper.queryCardById(cardId) != null){
            number = cardInfoMapper.deleteCardinfoById(cardId);
        }
        return number;
    }

    @Override
    public List<Cardinfo> queryCardByName(String CardName) {
        return cardInfoMapper.queryCardByName(CardName);
    }

    @Override
    public List<Cardinfo> getCardsByRandom(Integer n) {
        //保存卡片信息
        List<Cardinfo> cards = (List<Cardinfo>) this.getApplicationContext().getBean("ListCardinfoBean");
        cards.clear();
        //获取n次随机数
        List<Integer> randomNumbers = commonTools.getRandomNumbers(n);
        //根据每一个随机数,获取随机数对应的卡片信息
        for(Integer number:randomNumbers){
            Cardinfo cardinfo = cardInfoMapper.queryCardinfoByCardChance(number);
            //如果不为空,添加到卡中
            if(cardinfo != null) {
                cards.add(cardinfo);
            }else{
                cardinfo = (Cardinfo) this.getApplicationContext().getBean("cardinfoBean");
                cardinfo.setCardAddress("https://www.ali213.net/zt/genshiimpact/sim/images/wz.png");
                cardinfo.setCardName("其他");
                cards.add(cardinfo);
            }
        }
        return cards;
    }
}
